<template>
  <div style="height: 100%" class="workspace">
    <el-container ref="listWarp" style="height: 100%">
      <div class="left">
        <div class="title"><span></span> 数据字典</div>
        <div class="statusList">
          <div
            class="item"
            :class="statusCurrent === index ? 'activeItem' : ''"
            v-for="(item, index) in systemDictTypeList"
            :key="index"
            @click="activeClick(item, index)"
          >
            <svg-icon
              icon-class="info"
              style="font-size: 13px; margin-right: 3px"
            />
            {{ item.name }}
          </div>
        </div>
      </div>
      <el-container
        style="height: 100%; background: #fff; flex: 1"
        class="right"
      >
        <el-header style="height: auto; padding: 20px 20px 0 20px">
          <div class="title" style="border: none">
            <span></span> {{ systemDictName }}
          </div>
          <div class="selectList">
            <div class="select">
              <el-input
                v-model="searchObj.name"
                placeholder="请输入字典名称"
                size="small"
                @change="nameChange"
              ></el-input>
            </div>
            <div class="btns">
              <div class="add" @click="event('add')">
                <svg-icon
                  icon-class="add"
                  style="font-size: 13px; margin-right: 5px"
                />新增
              </div>
              <div class="edit" @click="event('edit')">
                <svg-icon
                  icon-class="edit"
                  style="font-size: 13px; margin-right: 5px"
                />

                编辑
              </div>
              <div class="del" @click="event('del')">
                <svg-icon
                  icon-class="delete"
                  style="font-size: 13px; margin-right: 5px"
                />

                删除
              </div>
            </div>
          </div>
        </el-header>
        <el-main style="height: 100%">
          <div class="content" style="height: 100%">
            <el-table
              :data="tableData"
              height="100%"
              style="width: 100%"
              border
              v-loading="loading"
              tooltip-effect="light"
              highlight-current-row
              @row-dblclick="tableDbEdit"
              @current-change="evtTableRowChange"
              @selection-change="handleSelectionChange"
              ref="dataListTable"
            >
              <el-table-column type="selection" width="55"> </el-table-column>
              <el-table-column
                type="index"
                width="50"
                label="序号"
              ></el-table-column>
              <el-table-column prop="name" label="字典值"> </el-table-column>
              <el-table-column prop="remark" label="备注"></el-table-column>
            </el-table>
          </div>
        </el-main>
      </el-container>
    </el-container>
    <el-dialog
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      @close="evtDialogClose"
      lock-scroll
      title="字典信息"
      width="450px"
    >
      <div v-loading="addLoading">
        <div class="win-warp" style="height: 230px">
          <el-form :model="form" :rules="addRules" ref="addForm">
            <el-row class="row-bg" type="flex">
              <el-col :span="20">
                <el-form-item
                  label="字典类型"
                  label-width="80px"
                  prop="typeName"
                >
                  <el-input
                    disabled
                    v-model="systemDictName"
                    readonly
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row class="row-bg" type="flex">
              <el-col :span="20">
                <el-form-item label="字典值" label-width="80px" prop="name">
                  <el-input
                    ref="name"
                    maxlength="30"
                    v-model="form.name"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row class="row-bg" type="flex">
              <el-col :span="20">
                <el-form-item label="排序号" label-width="80px" prop="sort">
                  <el-input maxlength="20" v-model="form.sort"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row class="row-bg" type="flex">
              <el-col :span="20">
                <el-form-item label="备注" label-width="80px" prop="remark">
                  <el-input maxlength="20" v-model="form.remark"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false" size="small"
            >取 消</el-button
          >
          <el-button
            @click="addBtn()"
            type="primary"
            size="small"
            style="background: #00b38b; border: none"
            >确定</el-button
          >
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { dictDataApi } from "@/api/patrol/dataDictionary";
export default {
  data() {
    return {
      statusCurrent: 0,
      tableData: [],
      multipleSelection: [],
      systemDictTypeList: [],
      loading: false,
      systemDictName: "",
      searchObj: {
        dictTypeId: "",
        name: "",
      },
      search: [],
      dialogFormVisible: false,
      form: {
        name: "",
        sort: "",
        remark: "",
        dictTypeId: "",
        typeName: "",
      },
      addLoading: false,
      typeName: "",
      currentRow: null,
      order: "sort asc",
      addRules: {
        name: [
          {
            required: true,
            message: "请填写字典值",
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            pattern: /^-?\d+\.?\d*$/,
            message: "请输入数字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 事件
    event(key) {
      switch (key) {
        case "add":
          this.dialogFormVisible = true;
          break;
        case "edit":
          if (!this.currentRow) {
            this.$message.error("请选中需要编辑的数据行");
            return;
          }
          this.getDetail();
          break;
        case "del":
          if (this.multipleSelection.length === 0) {
            this.$message.error("请选中要删除的数据");
            return;
          }
          this.del();
          break;
        default:
          break;
      }
    },
    // 弹窗关闭
    evtDialogClose() {
      this.dialogFormVisible = false;
      this.form = {};
    },
    // 新增
    addBtn() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.addLoading = true;
          this.form.dictTypeId = this.searchObj.dictTypeId;
          dictDataApi.save(this.form).then((res) => {
            this.addLoading = false;
            if (res.status == "success") {
              this.$message.success("提交成功");
              this.dialogFormVisible = false;
              this.addLoading = false;
              this.$refs.addForm.clearValidate();
              this.$refs.addForm.resetFields();
              this.getSystemDictData();
            }
          });
        } else {
          //   this.addLoading = false;
          return false;
        }
      });
    },
    // 详情
    getDetail() {
      this.dialogFormVisible = true;
      this.addLoading = true;
      dictDataApi
        .Get(this.currentRow.id)
        .then((res) => {
          this.form = res.data;
          this.addLoading = false;
        })
        .catch(() => {
          this.addLoading = false;
        });
    },
    // 删除
    del() {
      this.$confirm("确定要删除当前选中的数据吗？", "删除", {
        distinguishCancelAndClose: true,
        confirmButtonText: "删除",
        cancelButtonText: "放弃删除",
      })
        .then(() => {
          let idList = [];
          for (let i = 0; i < this.multipleSelection.length; i++) {
            idList.push(this.multipleSelection[i].id);
          }
          var ids = idList.join(",");
          dictDataApi.Del(ids).then((res) => {
            if ((res.status = "success")) {
              this.$message.success("删除成功");
              this.getSystemDictData();
            }
          });
        })
        .catch(() => {
          this.$refs.dataListTable.clearSelection();
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    tableDbEdit(currentRow) {
      //双击行
      this.currentRow = currentRow;
      this.getDetail();
    },
    evtTableRowChange(currentRow) {
      //单选行
      this.currentRow = currentRow;
    },
    activeClick(item, index) {
      this.statusCurrent = index;
      this.searchObj.dictTypeId = item.id;
      this.systemDictName = item.name;
      this.getSystemDictData();
    },
    nameChange() {
      this.getSystemDictData();
    },
    getSystemDictType() {
      dictDataApi.systemDictType().then((res) => {
        this.systemDictTypeList = res.data;
        if (this.systemDictTypeList.length > 0) {
          this.systemDictName = this.systemDictTypeList[0].name;
          this.searchObj.dictTypeId = this.systemDictTypeList[0].id;
          this.getSystemDictData();
        }
      });
    },
    setSearchData() {
      this.search = [];
      for (const key in this.searchObj) {
        const item = this.searchObj[key];
        if (item) {
          if (key === "dictTypeId") {
            this.search.push({
              andOr: "and",
              searchField: key,
              operator: "=",
              searchValue: item,
            });
          }
          if (key === "name") {
            this.search.push({
              andOr: "and",
              searchField: key,
              operator: "like",
              searchValue: item,
            });
          }
        }
      }
    },
    getSystemDictData() {
      this.loading = true;
      this.setSearchData();
      dictDataApi
        .systemDictData(JSON.stringify(this.search), this.order)
        .then((res) => {
          this.tableData = res.data;
          this.loading = false;
        });
    },
  },
  created() {
    this.getSystemDictType();
  },
};
</script>

<style lang="scss" scoped>
.workspace {
  .title {
    display: flex;
    align-items: center;
    column-gap: 8px;
    font-size: 16px;
    font-family: Alibaba PuHuiTi 2-65 Medium, Alibaba PuHuiTi 20;
    font-weight: normal;
    color: #1d2129;
    span {
      display: inline-block;
      width: 3px;
      height: 15px;
      background: #00b38b;
    }
    padding-bottom: 16px;
    border-bottom: 1px solid #eeeeee;
  }
  .left {
    width: 238px;
    background: #fff;
    margin-right: 10px;
    padding: 20px;

    .statusList {
      margin-top: 10px;
      .item {
        font-size: 14px;
        height: 42px;
        background: #fff;
        line-height: 42px;
        cursor: pointer;
        padding: 0 10px;
      }
      .activeItem {
        background: #e5f7f3;
        border-radius: 2px 2px 2px 2px;
        color: #00b38b;
      }
    }
  }
  .right {
    // padding: 20px;
    .selectList {
      display: flex;
      justify-content: space-between;
      .select {
        display: flex;
        column-gap: 10px;
      }
      .btns {
        display: flex;
        column-gap: 12px;
        div {
          height: 32px;
          border-radius: 2px 2px 2px 2px;
          font-size: 14px;
          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: #ffffff;
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 1px 15px;
          cursor: pointer;
        }
        .add {
          background: #00b38b;
        }
        .edit {
          background: #3d83ff;
        }
        .del {
          background: #ff5c5e;
        }
        .pass {
          background: #00b38b;
        }
        .reject {
          background: #ff5c5e;
        }
      }
    }
  }
}
</style>
